/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.fragments;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import pcgen.CharacterViewer.R;

public abstract class NumberPickerDialogFragment extends CharacterTabContentDialogFragment 
{
	public static NumberPickerDialogFragment newInstance(CharacterTabContentFragment fragment, Types type, int title, int current) 
    {
		return newInstance(fragment, type, title, current, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
	
	public static NumberPickerDialogFragment newInstance(CharacterTabContentFragment fragment, Types type, int title, int current, int min, int max) 
    {
        NumberPickerDialogFragment dialog = null;
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    	if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
    		dialog = new NumberPickerDialogFragmentDefault();
    	else
    		dialog = new NumberPickerDialogFragmentPrevious();
        
        // Pass args to dialog fragment...
        Bundle args = new Bundle();
        args.putInt("current", current);
        args.putInt("max", max);
        args.putInt("min", min);
        args.putInt("title", title);
        args.putString("type", type.toString());
        dialog.setArguments(args);
        
        initFragment(dialog, fragment);
	    
	    return dialog;
    }
	
	@Override
	public boolean doDialogNegativeClick()
	{
		return getTargetFragmentEx().dialogCancelNumberPicker();
	}
	
	@Override
	public boolean doDialogPositiveClick()
	{
		int value = getValue();
		if (_adjustmentDirection)
		{
			int temp = _current + value;
			if (temp > _max)
			{
				Toast.makeText(getActivity(), String.format(getString(R.string.dialog_validation_number_above_max), String.valueOf(temp), String.valueOf(_max)), Toast.LENGTH_SHORT).show();
				return false;
			}
		}
		else
		{
			if (_type == Types.Any)
			{
				int temp = (_current - value);
				if (temp < _min)
				{
					Toast.makeText(getActivity(), String.format(getString(R.string.dialog_validation_number_above_max), String.valueOf(temp), String.valueOf(_min)), Toast.LENGTH_SHORT).show();
					return false;
				}
			}
		}

		return getTargetFragmentEx().dialogCompleteNumberPicker(_type, value, _adjustmentDirection);
	}
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        // Get args...
        _current = getArguments().getInt("current");
        _max = getArguments().getInt("max");
        _min = getArguments().getInt("min");
        _title = getArguments().getInt("title");
        _type = Types.valueOf(getArguments().getString("type"));

        // Pick a style based on the num.
//	        int style = DialogFragment.STYLE_NORMAL, theme = 0;
//	        switch ((_num-1)%6) 
//	        {
//	            case 1: style = DialogFragment.STYLE_NO_TITLE; break;
//	            case 2: style = DialogFragment.STYLE_NO_FRAME; break;
//	            case 3: style = DialogFragment.STYLE_NO_INPUT; break;
//	            case 4: style = DialogFragment.STYLE_NORMAL; break;
//	            case 5: style = DialogFragment.STYLE_NORMAL; break;
//	            case 6: style = DialogFragment.STYLE_NO_TITLE; break;
//	            case 7: style = DialogFragment.STYLE_NO_FRAME; break;
//	            case 8: style = DialogFragment.STYLE_NORMAL; break;
//	        }
//	        switch ((_num-1)%6) {
//	            case 4: theme = android.R.style.Theme_Holo; break;
//	            case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
//	            case 6: theme = android.R.style.Theme_Holo_Light; break;
//	            case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
//	            case 8: theme = android.R.style.Theme_Holo_Light; break;
//	        }
//	        setStyle(style, theme);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
    {
    	final DialogFragment reference = this;
    	
    	View view = null;
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    	if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
    		view = inflater.inflate(R.layout.dialog_number_picker, container, false);
    	else
    		view = inflater.inflate(R.layout.dialog_number_picker_23x, container, false);
        
        getDialog().setTitle(getString(_title));
        
        _textCurrent = (TextView)view.findViewById(R.id.input_current);
        _textCurrent.setText(String.valueOf(_current));
        _textMax = (TextView)view.findViewById(R.id.input_max);
        _textMax.setText(String.valueOf(_max));
        _textMin = (TextView)view.findViewById(R.id.input_min);
        _textMin.setText(String.valueOf(_min));
        
        if (_type == Types.Initiative)
        	_textCurrent.setVisibility(View.VISIBLE);
        
        int max = _max;
        if (_type != Types.Initiative)
        {
        	max = _max - _current;
	        int min = Math.abs(_min - _current);
	        if (min > max)
	        	max = min;
        }
        
        initialize(view);
        setMaxValue(max);
        setMinValue(0);
//	        _picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() 
//	        {
//				public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
//				{
//					int add = _current + newVal;
//					_textMax.setText(String.valueOf(_max) + "/" + String.valueOf(add));
//					int subtract = _current - newVal;
//					_textMin.setText(String.valueOf(_min) + "/" + String.valueOf(subtract));
//				}
//			});

        _buttonAdd = (Button)view.findViewById(R.id.button_add);
        _buttonAdd.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	_adjustmentDirection = true;
            	if (doDialogPositiveClick())
            		reference.dismiss();
            }
        });

        _buttonSubtract = (Button)view.findViewById(R.id.button_subtract);
        _buttonSubtract.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	_adjustmentDirection = false;
            	if (doDialogPositiveClick())
            		reference.dismiss();
            }
        });

        _buttonMax = (Button)view.findViewById(R.id.button_max);
        _buttonMax.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	_adjustmentDirection = true;
            	int value = Math.abs(_max - _current);
            	setValue(value);
            }
        });

        _buttonReset = (Button)view.findViewById(R.id.button_reset);
        _buttonReset.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	setValue(0);
            }
        });
        
        _buttonOk = (Button)view.findViewById(R.id.button_ok);
        _buttonOk.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	if (doDialogPositiveClick())
            		reference.dismiss();
            }
        });
        
        _buttonCancel = (Button)view.findViewById(R.id.button_cancel);
        _buttonCancel.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	if (doDialogNegativeClick())
            		reference.dismiss();
            }
        });
        
        View layoutButtonsTop = (View)view.findViewById(R.id.layout_buttons_top);
        View layoutInfo = (View)view.findViewById(R.id.layout_info);
        
        if (_type == Types.Initiative)
        {
        	layoutInfo.setVisibility(View.GONE);
        	layoutButtonsTop.setVisibility(View.GONE);
        	_buttonAdd.setVisibility(View.GONE);
        	_buttonMax.setVisibility(View.GONE);
        	_buttonSubtract.setVisibility(View.GONE);
        	_buttonReset.setVisibility(View.VISIBLE);
        	_buttonOk.setVisibility(View.VISIBLE);
        }
        else
        {
        	layoutInfo.setVisibility(View.VISIBLE);
        	layoutButtonsTop.setVisibility(View.VISIBLE);
        	_buttonAdd.setVisibility(View.VISIBLE);
        	_buttonMax.setVisibility(View.VISIBLE);
        	_buttonSubtract.setVisibility(View.VISIBLE);
        	_buttonReset.setVisibility(View.VISIBLE);
        	_buttonOk.setVisibility(View.GONE);
        }

        return view;
    }
	
	protected abstract void initialize(View view);
	protected abstract int getValue();
	protected abstract void setMaxValue(int value);
	protected abstract void setMinValue(int value);
	protected abstract void setValue(int value);
	protected abstract void setVisibility(int value);
	
	private int _current;
	private int _max;
	private int _min;
	private int _title;
	private Types _type = Types.Any;
	
	private TextView _textCurrent;
	private TextView _textMax;
	private TextView _textMin;
	
	private boolean _adjustmentDirection;
	private Button _buttonAdd;
    private Button _buttonSubtract;
    private Button _buttonMax;
    private Button _buttonReset;
    private Button _buttonOk;
    private Button _buttonCancel;
    
	public enum Types
	{
		Any,
		Initiative
	}

	private static final String TAG = NumberPickerDialogFragment.class.getSimpleName();
}
